package com.bazaaroid.client.web.gwt.clientmodule.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

//import sun.misc.BASE64Encoder;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ClientModule_backup implements EntryPoint {
	
//	private SplitLayoutPanel mainPanel = new SplitLayoutPanel();
	private VerticalPanel mainPanel = new VerticalPanel();
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		// Create a three-pane layout with splitters. 
//		mainPanel.addWest(new HTML("navigacija - 200px"), 200);
		mainPanel.add(new HTML("ostatak"));
		mainPanel.add(new HTML("ostatak2"));
	    
		getClientData();
//		RootLayoutPanel.get().add(mainPanel);
		RootPanel.get("clientInfo").add(mainPanel);
	}
	
	 /**
	   * Convert the string of JSON into JavaScript object.
	   */
	  private final native JsArray<ClientData> asArrayOfClientData(String json) /*-{
	    return eval(json);
	  }-*/;
	  
	  private void getClientData() {
	// Send request to server and catch any errors.
		String url = "http://localhost:8888/rest/client/test";
		String username = "admin";
		String password = "pass";
//		String usrpass = username + ":" + PasswordService.encryptSha(password);
//		String encoded = new BASE64Encoder().encode(usrpass.getBytes());
		
	    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
	    builder.setHeader("Content-type","application/json; charset=utf-8");
//	    builder.setHeader("Authorization", "Basic " + encoded);
	    
	    try {
	      Request request = builder.sendRequest(null, new RequestCallback() {
	        public void onError(Request request, Throwable exception) {
	          displayError("Couldn't retrieve JSON");
	        }

	        public void onResponseReceived(Request request, Response response) {
	          if (200 == response.getStatusCode()) {
	            displayClientData(asArrayOfClientData(response.getText()));
	          } else {
	            displayError("Couldn't retrieve JSON (" + response.getStatusText()
	                + ")");
	          }
	        }
	      });
	    } catch (RequestException e) {
	      displayError("Couldn't retrieve JSON");
	    }
	  }
	  
	  /**
	   * If can't get JSON, display error message.
	   * @param error
	   */
	  private void displayError(String error) {
/*	    errorMsgLabel.setText("Error: " + error);
	    errorMsgLabel.setVisible(true);
	    */
	  }
	  
	  private void displayClientData(JsArray<ClientData> data) {
		    for (int i = 0; i < data.length(); i++) {
		      displayClientData(data.get(i));
		    }
		  }
	  
	  private void displayClientData(ClientData data ) {
		  mainPanel.add(new HTML(data.getName())); 
	  }

}
